1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35
| void CBook::DeleteDate(int iCount) { long respos; int iDataCount = 0; fstream file; fstream tmpfile; ofstream ofile; char cTempBuf[NUM1 + NUM1 + NUM2 + NUM2]; file.open("book.dat", ios::binary | ios::in | ios::out); tmpfile.open("temp.dat", ios::binary | ios::in | ios::out | ios::trunc); file.seekg(0, ios::end); respos = file.tellg(); iDataCount = respos / (NUM1 + NUM1 + NUM2 + NUM2); if (iCount < 0 && iCount > iDataCount) { throw "Input number error"; } else { file.seekg((iCount) * (NUM1 + NUM1 + NUM2 + NUM2), ios::beg); for (int j = 0; j < (iDataCount - iCount); j++) { memset(cTempBuf, 0, NUM1 + NUM1 + NUM2 + NUM2); file.read(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2); tmpfile.write(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2); } file.close(); tmpfile.seekg(0, ios::beg); ofile.open("book.dat"); ofile.seekp((iCount - 1) * (NUM1 + NUM1 + NUM2 + NUM2), ios::beg); for (int i = 0; i < (iDataCount - iCount); i++) { memset(cTempBuf, 0, NUM1 + NUM1 + NUM2 + NUM2); tmpfile.read(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2); ofile.write(cTempBuf, NUM1 + NUM1 + NUM2 + NUM2); } } tmpfile.close(); ofile.close(); remove("temp.dat"); }
|